package com.gc.web.controller.business;

import cn.hutool.core.collection.CollUtil;
import com.gc.business.domain.AnnualInspection;
import com.gc.business.domain.AnnualPlan;
import com.gc.business.service.IAnnualInspectionService;
import com.gc.business.service.IAnnualPlanService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.web.base.BaseController;
import com.gc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 电梯年检管理 控制层
 * 
 * @author hruiyan
 * @date 2020-12-01
 */
@Slf4j
@Controller
@RequestMapping("/business/annualInspection")
public class AnnualInspectionController extends BaseController {
    private String prefix = "business/annualInspection";

    @Autowired
    private ISysUserService sysUserService;
	
	private final IAnnualInspectionService annualInspectionService;
	private final IAnnualPlanService annualPlanService;

	@Autowired
	public AnnualInspectionController(IAnnualInspectionService annualInspectionService,
									  IAnnualPlanService annualPlanService) {
		this.annualInspectionService = annualInspectionService;
		this.annualPlanService = annualPlanService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:annualInspection:view")
	@GetMapping
	public String annualInspection(){
	    return prefix + "/annualInspection";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap){
		List<AnnualPlan> planList = annualPlanService.selectAnnualPlanList(new AnnualPlan(){{setCompanyId(getAttrId());}});
		mmap.put("planList",planList);
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{inspectionId}")
	public String edit(@PathVariable("inspectionId") Long inspectionId, ModelMap mmap){
		AnnualInspection annualInspection = annualInspectionService.getById(inspectionId);
		mmap.put("annualInspection", annualInspection);
		return prefix + "/edit";
	}
	
	/**
	 * 查询电梯年检管理列表
	 */
	@RequiresPermissions("business:annualInspection:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(AnnualInspection annualInspection){

		if(sysUserService.ifCompanyRole(getAttrId())){
			List<AnnualPlan> annualPlanList = annualPlanService.selectAnnualPlanList(new AnnualPlan() {{
				setCompanyId(getAttrId());
			}});
			if (CollUtil.isEmpty(annualPlanList) || annualPlanList.size() == 0){
				return Result.success();
			}
			List<Long> collect = annualPlanList.stream().map(AnnualPlan::getPlanId).collect(Collectors.toList());
			startPage();
			List<AnnualInspection> list = annualInspectionService.list(annualInspection,collect);
			return Result.success(getDataTable(list));
		}else{
			return Result.success(getDataTable(new ArrayList<AnnualInspection>()));
		}
	}

	/**
	 * 新增保存电梯年检管理
	 */
	@RequiresPermissions("business:annualInspection:add")
	@Log(title = "电梯年检管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(AnnualInspection annualInspection){
		AnnualPlan planById = annualPlanService.getById(annualInspection.getPlanId());
		//添加年检时间
		if(StringUtils.isNotNull(planById)){
			annualInspection.setAnnualDate(planById.getAnnualDate());
		}
		annualInspectionService.save(annualInspection);
		return Result.success();
	}

	/**
	 * 修改保存电梯年检管理
	 */
	@RequiresPermissions("business:annualInspection:edit")
	@Log(title = "电梯年检管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(AnnualInspection annualInspection){
		annualInspectionService.updateById(annualInspection);
		return Result.success();
	}
	
	/**
	 * 删除电梯年检管理
	 */
	@RequiresPermissions("business:annualInspection:remove")
	@Log(title = "电梯年检管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		annualInspectionService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出电梯年检管理列表
	 */
	@RequiresPermissions("business:annualInspection:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(AnnualInspection annualInspection){
		List<AnnualInspection> list = new ArrayList<>();
		if(sysUserService.ifCompanyRole(getAttrId())){
			List<AnnualPlan> annualPlanList = annualPlanService.selectAnnualPlanList(new AnnualPlan() {{
				setCompanyId(getAttrId());
			}});
			if (CollUtil.isEmpty(annualPlanList) || annualPlanList.size() == 0){
				return Result.success();
			}
			List<Long> collect = annualPlanList.stream().map(AnnualPlan::getPlanId).collect(Collectors.toList());
			list = annualInspectionService.list(annualInspection, collect);
		}
		ExcelUtil<AnnualInspection> util = new ExcelUtil<>(AnnualInspection.class);
		util.exportExcel(list, "annualInspection");
		return Result.success();
	}
}
